package com.jkl.controller.dept;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.jkl.entity.BaseResponse;
import com.jkl.entity.dept.Dept;
import com.jkl.service.dept.inter.DeptServiceInterface;
import org.apache.catalina.filters.ExpiresFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.dept.BSDept;
import com.jkl.service.dept.inter.BSDeptServiceInterface;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * <p>
 * 123开放层接口
 * <p>
 * <p>
 * 变更说明：linpf 2017年12月28日 123 增加新增、更新、删除方法
 * <p>
 * Date:2018年10月07日
 * 
 * @author lin.pf
 * @version 1.0
 */
@RestController
@RequestMapping("/jky/dept")
@Slf4j
public class BSDeptControllerV1 {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private BSDeptServiceInterface bsDeptServiceInterface;

	@Autowired
	private DeptServiceInterface deptServiceInterface;

	@GetMapping("/getCityDept")
	public BaseResponseDTO getCityDept(@RequestParam(name = "deptId") Integer deptId) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSDept> deptList = bsDeptServiceInterface.queryParamByGroupCode(deptId);
		JSONObject returnJson = new JSONObject();
		JSONArray deptArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", deptArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/getDeptById")
	public BaseResponseDTO getDeptById(@RequestBody BSDept param) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSDept> deptList = bsDeptServiceInterface.getDeptById(param.getRowId(), param.getParentId());
		JSONObject returnJson = new JSONObject();
		JSONArray deptArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", deptArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@GetMapping("/getAreaByDeptId")
	public BaseResponseDTO getAreaByDeptId(@RequestParam(value = "deptId") String deptId) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		BSDept areaInfo = bsDeptServiceInterface.getAreaByDeptId(deptId);
		JSONObject returnJson = (JSONObject) JSON.toJSON(areaInfo);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@GetMapping("/getDeptBylevel")
	public BaseResponseDTO getDeptBylevel(@RequestParam(value = "level") int level) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		System.out.println(level);
		List<BSDept> depts = bsDeptServiceInterface.getDeptBylevel(level);
		JSONObject returnJson = new JSONObject();
		JSONArray returnArray = (JSONArray) JSONArray.toJSON(depts);
		returnJson.put("deptList", returnArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/updateDept")
	public BaseResponseDTO updateDept(@RequestBody BSDept dept) {
		bsDeptServiceInterface.updateDept(dept);
		return new BaseResponseDTO();
	}

	@PostMapping("/addDept")
	public BaseResponseDTO addDept(@RequestBody BSDept dept) {
		bsDeptServiceInterface.addDept(dept);
		return new BaseResponseDTO();
	}

	@PostMapping("/deleteDept")
	public BaseResponseDTO deleteDept(@RequestBody BSDept dept) {
		bsDeptServiceInterface.deleteDept(dept);
		return new BaseResponseDTO();
	}

	@PostMapping("/getDeptList")
	public BaseResponseDTO getDeptList() {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSDept> deptList = bsDeptServiceInterface.getDeptList();
		JSONObject returnJson = new JSONObject();
		JSONArray deptArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", deptArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/getDeptObject") // 获取单个
	public List<BSDept> getDeptObject(@RequestParam(value = "rowId") String rowId) {
		return bsDeptServiceInterface.getDeptObject(rowId);
	}

	@PostMapping("/getDeptObjectLevel4") // 获取第四级别组织机构
	public List<BSDept> getDeptObjectLevel4(@RequestParam(value = "rowId") String rowId) {
		return bsDeptServiceInterface.getDeptObjectLevel4(rowId);
	}

	@PostMapping("/getDeptObjectlevel") // 根据名称获取级别
	public BSDept getDeptObjectlevel(@RequestParam(value = "cityName") String cityName) {
		return bsDeptServiceInterface.getDeptObjectlevel(cityName);
	}
	/**
	 * @author xgs
	 * @return
	 * @description  根据父id查询其子节点(在pj_project中不重复的)
	 * @time 2021/4/24 15:56
	 */
	@PostMapping("/getChildrenListByParentId")
	public List<BSDept> getChildrenList(@RequestParam(value = "parentId") Integer parentId) {
		return bsDeptServiceInterface.getChildrenListByParentId(parentId);
	}

	@PostMapping("/queryAddress") // 根据父ID查所有的子节点
	public List<BSDept> queryAddress(@RequestParam(value = "parentId") Integer parentId) {
		return bsDeptServiceInterface.queryAddress(parentId);
	}

	@GetMapping("/getDeptByRoleType")
	public BaseResponseDTO getDeptByRoleType(@RequestParam(value = "roleType") int roleType) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BSDept> depts = bsDeptServiceInterface.getDeptByRoleType(roleType);
		JSONObject returnJson = new JSONObject();
		JSONArray returnArray = (JSONArray) JSONArray.toJSON(depts);
		returnJson.put("deptList", returnArray);
		returnData.setReturnData(returnJson);
		return returnData;
	}

	@PostMapping("/getBSDeptStreet")
	public List<BSDept> getBSDeptStreet(@RequestParam(name = "deptId") String deptId) {
		return bsDeptServiceInterface.getBSDeptStreet(deptId);
	}

	@PostMapping("/getBSDeptCommunity")
	public List<BSDept> getBSDeptCommunity(@RequestParam(name = "deptId") String deptId) {
		return bsDeptServiceInterface.getBSDeptCommunity(deptId);
	}


//	@GetMapping("/getAllDept")
//	public List<Dept> getAllDept() {
//		List<Dept> deptList = deptServiceInterface.getDeptList();
//		return deptList;
//	}

	//TODO 五级组织机构
	@PostMapping("/list/tree")
	public BaseResponseDTO listTreeById(@RequestParam(name = "deptId") String deptId){
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<Dept> deptList = deptServiceInterface.listWithTree(deptId);
		JSONObject returnJson = new JSONObject();
		JSONArray returnArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", returnArray);
		return returnData.setReturnData(returnJson);
	}

	//市县
	@PostMapping("/citiesCounties")
	public BaseResponseDTO citiesCountiesById(@RequestParam(name = "deptId") String deptId){
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<Dept> deptList = deptServiceInterface.citiesCounties(deptId);
		JSONObject returnJson = new JSONObject();
		JSONArray returnArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", returnArray);
		return returnData.setReturnData(returnJson);
	}

	//这个方法主要返回组织机构，是为了查询根节点一直为嘉兴市
	@PostMapping("/deptWithTree")
	public BaseResponseDTO deptWithTree(@RequestParam String deptId){
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<Dept> deptList = deptServiceInterface.rootDeptList(deptId);
		JSONObject returnJson = new JSONObject();
		JSONArray returnArray = (JSONArray) JSONArray.toJSON(deptList);
		returnJson.put("deptList", returnArray);
		return returnData.setReturnData(returnJson);
	}
//	@PostMapping("/getDeptWithTree")
//	public List<Dept> getDeptToTree() {
//		List<Dept> deptList = deptServiceInterface.getDeptList();
//		deptList.stream().
//		return deptList;
//	}
}